package com.mqqzt.douban;

import java.util.List;

import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gdata.data.Link;
import com.google.gdata.data.TextContent;
import com.google.gdata.data.douban.UserEntry;
import com.mqqzt.douban.util.LoadImageAsyncTask;
import com.mqqzt.douban.util.LoadImageAsyncTask.LoadImageCallback;

public class MyInfoActivity extends BaseActivity implements OnClickListener {
	private TextView mTextViewName;
	private TextView mTextViewLocation;
	private TextView mTextViewInfo;
	private ImageView mImageViewIcon;
	private String name;
	private String location;
	private String content;
	private String imageurl;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.my_info);
		super.onCreate(savedInstanceState);
	}
	@Override
	public void setupView() {
		mTextViewName = (TextView) this.findViewById(R.id.txtUserName);
		mTextViewLocation = (TextView) this.findViewById(R.id.txtUserAddress);
		mTextViewInfo = (TextView) this.findViewById(R.id.txtUserDescription);
		mImageViewIcon = (ImageView) this.findViewById(R.id.imgUser);
		mTextViewTitle = (TextView) this.findViewById(R.id.myTitle);
		mRelativeLayoutLoading = (RelativeLayout) this.findViewById(R.id.loading);
		mImageBack = (ImageButton) this.findViewById(R.id.back_button);
	}

	@Override
	public void setListener() {
		mImageBack.setOnClickListener(this);
	}

	@Override
	public void fillData() {
		mTextViewTitle.setText("我的资料");
		new AsyncTask<Void, Void, Void>() {
			
			@Override
			protected void onPreExecute() {
				showLoading();
				super.onPreExecute();
			}

			@Override
			protected void onPostExecute(Void result) {
				hideLoading();
				mTextViewName.setText(name);
				mTextViewLocation.setText(location);
				mTextViewInfo.setText(content);
				LoadImageAsyncTask task = new LoadImageAsyncTask(new LoadImageCallback() {
					
					@Override
					public void beforeLoadImage() {
						mImageViewIcon.setImageResource(R.drawable.app_icon);
						
					}
					
					@Override
					public void afterLoadImage(Bitmap bitmap) {
						if(bitmap != null){
							mImageViewIcon.setImageBitmap(bitmap);
						}else{
							mImageViewIcon.setImageResource(R.drawable.app_icon);
						}
						
					}
				});
				task.execute(imageurl);
				super.onPostExecute(result);
			}

			@Override
			protected Void doInBackground(Void... params) {
				try {
					UserEntry ue = myService.getAuthorizedUser();
					name = ue.getTitle().getPlainText();
					location = ue.getLocation();
					content = ((TextContent) ue.getContent()).getContent().getPlainText();
					List<Link> links = ue.getLinks();
					for(Link link : links){
						if("icon".equals(link.getRel())){
							imageurl = link.getHref();
						}
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
				return null;
			}
		}.execute();
	}
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.back_button:
			finish();
			break;

		}
		
	}

}
